package com.dobbinsoft.iotechn.data.client;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoInfoResponse;
import com.dobbinsoft.iotechn.core.exception.CoreExceptionDefinition;
import com.dobbinsoft.iotechn.core.exception.ServiceException;
import com.dobbinsoft.iotechn.data.exception.DataExceptionDefinition;
import com.dobbinsoft.iotechn.data.exception.DataServiceException;
import com.dobbinsoft.iotechn.data.logger.Logger;
import com.dobbinsoft.iotechn.data.logger.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.InputStream;
import java.util.List;

/**
 * @author kbq
 * @ClassName: AliyunVideoClient
 * @Description:
 * @date 20-3-25下午2:49
 */

public class AliyunVideoClient implements VideoClient {

    private static final Logger logger = LoggerFactory.getLogger(AliyunVideoClient.class);

    private String accessKeyId;
    private String accessKeySecret;
    private String regionId; // 点播服务接入区域
    public void setAccessKeyId(String accessKeyId) {
        this.accessKeyId = accessKeyId;
    }
    public void setAccessKeySecret(String accessKeySecret) {
        this.accessKeySecret = accessKeySecret;
    }
    public void setRegionId(String regionId) {
        this.regionId = regionId;
    }

    @Autowired
    private DefaultAcsClient defaultAcsClient;

    /**
     * 获取播出地址
     * @param videoId
     * @return
     * @throws ServiceException
     */
    @Override
    public String getPlayInfo(String videoId) throws ServiceException {
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        request.setVideoId(videoId);
        request.setFormats("mp4");

        GetPlayInfoResponse response = null;
        try {
            response = defaultAcsClient.getAcsResponse(request);
            List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
            if(CollectionUtils.isEmpty(playInfoList)){
                throw new DataServiceException(DataExceptionDefinition.GET_PLAY_ADDRESS_FALI_EMPTY);
            } else {
                String playAddress = null;
                //播放地址
                for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
                    playAddress = playInfo.getPlayURL();
                    break;
                }
                return playAddress;
            }
        } catch (ClientException e){
            logger.error("[获取视频地址] 异常", e);
            throw new DataServiceException(DataExceptionDefinition.GET_PLAY_ADDRESS_FALI_CLIENT);
        }
    }


    /**
     * 视频流上传接口
     *
     * @param title
     * @param fileName
     * @param is
     */
    @Override
    public String uploadVideo(String title, String fileName, InputStream is) throws ServiceException{
        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadStreamRequest request = new UploadStreamRequest(this.accessKeyId, this.accessKeySecret, title, fileName, is);
        request.setApiRegionId(this.regionId); // 点播服务接入区域
        UploadStreamResponse response = uploader.uploadStream(request);

        if(!StringUtils.isEmpty(response.getVideoId())){
            return response.getVideoId();
        }else {
            throw new DataServiceException(CoreExceptionDefinition.buildVariableException(DataExceptionDefinition.UPLOAD_VIDEO_REQUEST_FAIL,response.getMessage()));
        }
    }

    /**
     * 查询
     */
    @Override
    public  String getCover(String videoId) throws ServiceException{
        GetVideoInfoRequest request = new GetVideoInfoRequest();
        request.setVideoId(videoId);
        try {
            GetVideoInfoResponse acsResponse = defaultAcsClient.getAcsResponse(request);
            if(acsResponse.getVideo() == null || StringUtils.isEmpty(acsResponse.getVideo().getCoverURL())) {
                throw new DataServiceException(DataExceptionDefinition.GET_COVER_FAIL_EMPTY);
            }
            return acsResponse.getVideo().getCoverURL();
        } catch (ServiceException e) {
            throw e;
        } catch (ClientException e){
            throw new DataServiceException(DataExceptionDefinition.GET_COVER_FAIL_CLIENT);
        }
    }
}

